﻿@model Dictionary<string, string>
@{
    int pageIndex = 1;  //页码，从1开始
    int pageSize = 15;  //页大小
    int recordCount = 0;//总记录数
    int indexCount = 19;//显示页码的个数
    string controllerName = Model["Controller"];//控制器名称
    string actionName = Model["Action"];//活动名称
    string paremeters = null;//参数
    string tagid = null;
    if (Model.ContainsKey("Paremeters"))
    {
        paremeters = Model["Paremeters"];
    }
    if (Model.ContainsKey("tagid"))
    {
        tagid = Model["tagid"];
    }
    if (Model.ContainsKey("PageIndex"))
    {
        pageIndex = int.Parse(Model["PageIndex"]);
    }
    if (Model.ContainsKey("PageSize"))
    {
        pageSize = int.Parse(Model["PageSize"]);
    }
    if (Model.ContainsKey("RecordCount"))
    {
        recordCount = int.Parse(Model["RecordCount"]);
    }
    if (Model.ContainsKey("IndexCount"))
    {
        indexCount = int.Parse(Model["IndexCount"]);
    }

    int pageCount = recordCount / pageSize;
    if (recordCount % pageSize > 0)
    {
        pageCount++;
    }

    if (pageCount < 2)
    {
        return;
    }
    int preIndex = 1;
    if (pageIndex > 2)
    {
        preIndex = pageIndex - 1;
    }
    int nextIndex = pageCount;
    if (pageIndex < pageCount)
    {
        nextIndex = pageIndex + 1;
    }
    int preCount = indexCount / 2;
    int nextCount = indexCount - preIndex;

    int minIndex = 1;
    int maxIndex = pageCount;
    if (indexCount <= pageCount)
    {
        minIndex = Math.Max((pageIndex - ((indexCount - 1) / 2)), 1);
        maxIndex = Math.Min(indexCount + minIndex, pageCount);
        if (maxIndex == pageCount)
        {
            minIndex = Math.Max(1, pageCount - indexCount);
        }
    }
}
<div class="pagination pagination-centered">
    <ul>
        @if (pageIndex > 1)
        {
            //首页，上一页
            <li>@Html.ActionLink("上一页", actionName, controllerName, new { id = paremeters, tagid = tagid, p = preIndex }, null)</li>
        }
        @for (int i = minIndex; i < pageIndex; i++)
        { 
            <li>@Html.ActionLink(i.ToString(), actionName, controllerName, new { id = paremeters, tagid = tagid, p = i }, null)</li>
        }
        <li class="active"><a href="#">@pageIndex</a> </li>
        @for (int i = pageIndex + 1; i <= maxIndex; i++)
        {
            <li>@Html.ActionLink(i.ToString(), actionName, controllerName, new { id = paremeters, tagid = tagid, p = i }, null)</li>
        }
        @if (pageIndex < pageCount)
        {
            //下一页，末页
            <li>@Html.ActionLink("下一页", actionName, controllerName, new { id = paremeters, tagid = tagid, p = nextIndex }, null)</li>
        }
    </ul>
</div>
